//
//  LeftViewController.m
//  ProjectA_V0.1
//
//  Created by liuchang on 14/12/29.
//  Copyright (c) 2014年 liuchang. All rights reserved.
//

#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)

#import "LeftViewController.h"

@interface LeftViewController ()

@end

@implementation LeftViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {

    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUI];
    [self addRecognizer];
}

- (void)setUI
{
    [self.view setFrame:CGRectMake(- SCREEN_WIDTH * 0.382, 0, SCREEN_WIDTH * 0.382, SCREEN_HEIGHT)];
    [self.view setBackgroundColor:[UIColor colorWithWhite:0.05 alpha:0.95]];
}

- (void)addRecognizer
{
    UIPanGestureRecognizer * panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(recognizerAction:)];
    [self.view addGestureRecognizer:panRecognizer];
    [panRecognizer release];
}

- (void)recognizerAction:(id)sender
{
    UIPanGestureRecognizer * panRecognizer = (UIPanGestureRecognizer *)sender;
    CGPoint translation = [panRecognizer translationInView:panRecognizer.view];
        if (UIGestureRecognizerStateBegan == panRecognizer.state)
        {
#warning 快速拖动
        }
        if (UIGestureRecognizerStateBegan == panRecognizer.state ||
            UIGestureRecognizerStateChanged == panRecognizer.state) {
            if (self.view.frame.origin.x + translation.x < 0) {
                self.view.center = CGPointMake(self.view.center.x + translation.x, self.view.center.y);
                [panRecognizer setTranslation:CGPointMake(0, 0) inView:self.view];
            }
            else {
                [self.view setFrame:CGRectMake(0, 0, SCREEN_WIDTH * 0.382, SCREEN_HEIGHT)];
            }
        }
        if (UIGestureRecognizerStateEnded == panRecognizer.state) {
            if (self.view.frame.origin.x < - 50) {
                [UIView animateWithDuration:0.3 animations:^{
                    [self.view setFrame:CGRectMake(- 0.382 * SCREEN_WIDTH, 0, SCREEN_WIDTH * 0.382, SCREEN_HEIGHT)];
                    self.endBlock();
                }];
            }
            else {
                [UIView animateWithDuration:0.3 animations:^{
                    [self.view setFrame:CGRectMake(0, 0, SCREEN_WIDTH * 0.382, SCREEN_HEIGHT)];
                }];

            }
        }

}

- (void)endGetBlock:(EndBlock)endBlock
{
    self.endBlock = endBlock;
}

@end
